/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities1.Event;
import entities1.Invited;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.inject.Inject;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author takamflore
 */
public class SendEmail {
    @Inject
    EventDao eventDao;
    @Inject
    InvitedDao invited;  
    Event event;
    List<Invited> listI;
    ArrayList< String> emails;
    String host, port, emailid,username, password;
    Properties props = System.getProperties();
    Session l_session = null;
    
    public SendEmail(){
        invited= new InvitedDaoImpl();
    }

    public void BSendMail(Event event) {
        host = "smtp.mail.yahoo.com,mail.smtp.host, smtp.gmail.com";
        port = "587";
        emailid = "msuffoyelena@gmail.com";
        password = "pwd";
        emailSettings();
        createSession();
        sendMessage();
    }
 public void emailSettings() {
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "false");
        props.put("mail.smtp.port", port);
 }
 
   public void createSession() {

        l_session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(emailid, password);
                    }
                });

        l_session.setDebug(true); // Enable the debug mode

    }

 public void sendMessage(){

        try {
            MimeMessage message = new MimeMessage(l_session);
            message.setFrom(new InternetAddress(emailid));  
           message.setRecipients(Message.RecipientType.BCC, getEmailsList(emails));
           message.setSubject(event.getNameEvent());          
           message.setText(event.getDescription());
            Transport.send(message);
            
        } catch (MessagingException ex) {
           ex.printStackTrace();
        }
 }
 
 public List<String> getEmails(Event event){
     
     listI= invited.getAllEventInvited(event.getId());
     for(Invited i:listI){
         emails.add(i.getUser().getEmail());
     }
     return emails;
 }
 private Address[] getEmailsList(ArrayList< String > emails) {
 
  Address[] emaiAddresses = new Address[emails.size()];
 
  for (int i =0;i < emails.size();i++) {
   try {
    emaiAddresses[i] = new InternetAddress(emails.get(i));
   }
   catch (AddressException e) {
 
    e.printStackTrace();
   }
  }
  return emaiAddresses;
 }
}
